﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication2
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
        private bool moving=false;
        public enum vitricuadiemthaydoi
        {
            between_left,top_left,none
        }
        public enum option
        {
            move,changersize,none
        }
        private vitricuadiemthaydoi position_changer = vitricuadiemthaydoi.none;
        Point start;
        private option kiemtra = option.none;
        Rectangle rectanger_selected;
        Point between=new Point();
        Point topleft = new Point();
        Ellipse elipse;
        private void add_rectanger_Click(object sender, RoutedEventArgs e)
        {
            elipse = new Ellipse();
            Rectangle rectanger = new Rectangle();
            rectanger.Stroke = new SolidColorBrush(Colors.Red);
            rectanger.Fill = new SolidColorBrush(Colors.Yellow);
            rectanger.StrokeThickness = 2;
            rectanger.Width = 200;
            rectanger.Height = 250;
            canvas1.Children.Add(rectanger);
            rectanger.MouseLeftButtonDown += new MouseButtonEventHandler(rectanger_MouseLeftButtonDown);
            rectanger.MouseLeftButtonUp += new MouseButtonEventHandler(rectanger_selected_MouseLeftButtonUp);
        }

        void rectanger_selected_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            kiemtra = option.none;
        }

       

        void rectanger_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            rectanger_selected = (Rectangle)sender;
            start = e.GetPosition(rectanger_selected);
            Point vitricuaanh = new Point();
            vitricuaanh.X = (double)rectanger_selected.GetValue(Canvas.LeftProperty);
            vitricuaanh.Y = (double)rectanger_selected.GetValue(Canvas.TopProperty);
            topleft.X = vitricuaanh.X;
            topleft.Y = vitricuaanh.Y;
            between.X = vitricuaanh.X;
           
            between.Y=vitricuaanh.Y + rectanger_selected.Height * 0.5;
            Point current = e.GetPosition(canvas1);
            double khoangcach_topleft = khoangcach(topleft, current);
            double khoang_cach = khoangcach(current, between);
            if (khoang_cach<20)
            {
                kiemtra = option.changersize;
               
                position_changer = vitricuadiemthaydoi.between_left;
            }
            else if (khoangcach_topleft<20)
            {
                kiemtra=option.changersize;
                position_changer=vitricuadiemthaydoi.top_left;
            }
            else if (khoangcach_topleft>20||khoang_cach>20)
            {
               // kiemtra = option.move;
            }
            
            
             
           
        }

        private void canvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            moving = true;
            Point current = e.GetPosition(canvas1);
            double khoangcach_topleft = khoangcach(topleft, current);
            double khoang_cach = khoangcach(between, current);
            if (khoang_cach<40)
            {
                kiemtra = option.changersize;
                position_changer = vitricuadiemthaydoi.between_left;
            }else if (khoangcach_topleft<40)
            {
                kiemtra = option.changersize;
                position_changer = vitricuadiemthaydoi.top_left;
            }
            else if (khoang_cach>20||khoangcach_topleft>40)
            {
                kiemtra = option.move;
                elipse.Visibility = Visibility.Collapsed;
            }
            
           

        }

        private void canvas1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            
            moving = false;
            elipse.Visibility = Visibility.Collapsed;
            //rectanger_selected = (Rectangle)sender;
            topleft.X =(double) rectanger_selected.GetValue(Canvas.LeftProperty);
            topleft.Y = (double)rectanger_selected.GetValue(Canvas.TopProperty);
            between.X = topleft.X;
            between.Y = topleft.Y +  rectanger_selected.Height/2;
            
        }
        /// <summary>
        /// tinh khoang cach cua hai diem
        /// </summary>
        /// <param name="diem1"></param>
        /// <param name="diem2"></param>
        /// <returns></returns>
        private double khoangcach(Point diem1,Point diem2)
        {
            return Math.Sqrt(Math.Pow(diem1.X - diem2.X, 2) + Math.Pow(diem1.Y - diem2.Y, 2));
        }
        private void canvas1_MouseMove(object sender, MouseEventArgs e)
        {
            switch (kiemtra)
            {
                case option.move:
                   
                    {
                        if (moving==true)
                           { 
                                Point current = e.GetPosition(canvas1);
                                double x = current.X - start.X;
                                double y = current.Y - start.Y;
                                rectanger_selected.SetValue(Canvas.LeftProperty, x);
                                rectanger_selected.SetValue(Canvas.TopProperty, y);
                                Point vitricuaanh = new Point();
                                topleft.X=x;
                                topleft.Y=y;
                                vitricuaanh.X = (double)rectanger_selected.GetValue(Canvas.LeftProperty);
                                vitricuaanh.Y = (double)rectanger_selected.GetValue(Canvas.TopProperty);
                                between = new Point(x, y + rectanger_selected.Height/2);
                                elipse.SetValue(Canvas.LeftProperty, between.X - 10);
                                elipse.SetValue(Canvas.TopProperty, between.Y - 10);
                           }
                       
                    }
                    break;
                case option.none:
                    {
                        if (elipse==null)
                        {
                            return;
                        }
                        Point current = e.GetPosition(canvas1);
                        double khoangcach_topleft = khoangcach(current, topleft);
                        double khoang_cach = khoangcach(between, current);
                        if (khoang_cach<40)
                        {
                            elipse.Stroke = new SolidColorBrush(Colors.Red);
                            elipse.StrokeThickness = 1.5;
                            elipse.Width = 20;
                            elipse.Height = 20;
                            elipse.SetValue(Canvas.LeftProperty, between.X - 10);
                            elipse.SetValue(Canvas.TopProperty, between.Y - 10);
                            if (canvas1.Children.Contains(elipse))
                            {
                                elipse.Visibility = Visibility.Visible;
                            }
                            else
                            {
                                canvas1.Children.Add(elipse);
                            }
                        }else if (khoangcach_topleft<40)
                        {
                            //elipse.Stroke = new SolidColorBrush(Colors.Red);
                            //elipse.StrokeThickness = 1.5;
                            //elipse.Width = 20;
                            //elipse.Height = 20;
                            elipse.SetValue(Canvas.LeftProperty, topleft.X - 10);
                            elipse.SetValue(Canvas.TopProperty,topleft.Y- 10);
                            if (canvas1.Children.Contains(elipse))
                            {
                                elipse.Visibility = Visibility.Visible;
                            }
                            else
                            {
                                canvas1.Children.Add(elipse);
                            }
                        }
                        else
                        {
                            elipse.Visibility = Visibility.Collapsed;
                        }
                       
                    }
                    break;
                case option.changersize:
                    {
                        if (moving==true)
                        {
                           switch (position_changer)
                           {
                               case vitricuadiemthaydoi.between_left:
                                   {
                                       Point current = e.GetPosition(canvas1);
                                       double x = current.X - between.X;
                                       double y = current.Y - between.Y;
                                       if (rectanger_selected.Width - x < 3 )
                                       {
                                           return;
                                       }
                                       rectanger_selected.Width -= x;
                                       rectanger_selected.SetValue(Canvas.LeftProperty, current.X);
                                       topleft.X = current.X;
                                       between.X = current.X;
                                       elipse.SetValue(Canvas.LeftProperty, between.X - 10);
                                       elipse.SetValue(Canvas.TopProperty, between.Y - 10);
                                   }
                                   break;
                               case vitricuadiemthaydoi.top_left:
                                   {
                                       Point current = e.GetPosition(canvas1);
                                       double x = current.X - topleft.X;
                                       double y = current.Y - topleft.Y;
                                       //if (rectanger_selected.Width - x < 3|| rectanger_selected.Height-3<0)
                                       //{
                                       //    return;
                                       //}
                                       rectanger_selected.Width -= x;
                                       rectanger_selected.Height-=y;
                                       rectanger_selected.SetValue(Canvas.LeftProperty, current.X);
                                       rectanger_selected.SetValue(Canvas.TopProperty, current.Y);
                                       topleft.X = current.X;
                                       topleft.Y = current.Y;
                                       between.X = current.X;
                                       between.Y = current.Y + rectanger_selected.Height * 0.5;
                                       elipse.SetValue(Canvas.LeftProperty, topleft.X - 10);
                                       elipse.SetValue(Canvas.TopProperty, topleft.Y - 10);
                                   }
                                   break;
                           }
                        }
                    }
                    break;
            }
        }
    }
}
